﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using LINQPad.OpenAccess.Metadata;
using Telerik.OpenAccess.Metadata;

namespace LINQPad.OpenAccess.Settings
{
    /// <summary>
    /// Metadata Source settings.
    /// </summary>
    [DataContract( Name = "MetadataSource", Namespace = "" )]
    public class MetadataSourceSettings : IMetadataSourceInfo, IEquatable<IMetadataSourceInfo>
    {
        #region Public

        #region - Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MetadataSourceSettings" /> class.
        /// </summary>
        public MetadataSourceSettings( ) { }
        /// <summary>
        /// Initializes a new instance of the <see cref="MetadataSourceSettings" /> class.
        /// </summary>
        /// <param name="metadataSourceInfo">The metadata source info.</param>
        public MetadataSourceSettings( IMetadataSourceInfo metadataSourceInfo )
        {
            if ( metadataSourceInfo != null )
            {
                this.MappingType = metadataSourceInfo.MappingType;
                this.Name = metadataSourceInfo.Name;
                this.Path = metadataSourceInfo.Path;
            }
        }

        #endregion

        #endregion

        #region Object Members

        /// <summary>
        /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals( object obj )
        {
            return this.Equals( obj as IMetadataSourceInfo );
        }
        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode( )
        {
            return MetadataSourceInfoEqualityComparer.Default.GetHashCode( this );
        }
        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString( )
        {
            return String.Format( "{0}.{1} ({2})", this.Path, this.Name, this.MappingType );
        }

        #endregion

        #region IMetadataSourceInfo Members

        /// <summary>
        /// Gets the type of the mapping.
        /// </summary>
        /// <value>
        /// The type of the mapping.
        /// </value>
        [DataMember]
        public MappingDefinitionType MappingType { get; set; }
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        [DataMember]
        public string Name { get; set; }
        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <value>
        /// The path.
        /// </value>
        [DataMember]
        public string Path { get; set; }

        #endregion

        #region IEquatable<IMetadataSourceInfo> Members

        /// <summary>
        /// Determines whether the current instance is logically equal to the specified other instance.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns>
        ///   <c>true</c> if equal; otherwise, <c>false</c>.
        /// </returns>
        public bool Equals( IMetadataSourceInfo other )
        {
            return MetadataSourceInfoEqualityComparer.Default.Equals( this, other );
        }

        #endregion
    }
}
